//
//  XGardenNewsBigImgCell.swift
//  WeiBoReDo
//
//  Created by 放学跟鸽走 on 2017/4/15.
//  Copyright © 2017年 放学跟鸽走. All rights reserved.
//

import UIKit

/// 鸽闻园地中的单图大图CellView:上面标题，中间大图，左下作者，右下评论
class XGardenNewsBigImgCell: UITableViewCell {
    
    var viewModel:XGardenNewsViewModel?{
        didSet{
            let news = viewModel?.gardenNews
            imgView.cz_setImage(urlString: news?.img1, placeholderImage: nil)
            titleView.text = news?.title
            authorView.text = news?.author
            commentView.text = String.commentString(count: news?.plnum ?? 0, defaultStr: "0评")
        }
    }
    
    /// 大图
    lazy var imgView:UIImageView = UIImageView()
    
    /// 标题
    lazy var titleView:UILabel = UILabel.cz_label(withText: "标题", fontSize: 14, color: UIColor.black)
    
    /// 作者
    lazy var authorView:UILabel = UILabel.cz_label(withText: "作者", fontSize: 12, color: UIColor.cz_color(withRed: 163, green: 163, blue: 163))
    /// 评论数
    lazy var commentView:UILabel = UILabel.cz_label(withText: "100评", fontSize: 12, color: UIColor.cz_color(withRed: 163, green: 163, blue: 163))
    
    /// 底部分隔线
    lazy var bottomLine:UIView = UIView()
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupUI()
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        setupUI()
        
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        
    }
    
}

// MARK: - UI搭建
extension XGardenNewsBigImgCell{
    
    
    /// UI搭建
    func setupUI(){
        selectionStyle = .none
        //        backgroundColor = UIColor.cz_random()
        //添加view
        addViewToCell()
        //自定义布局
        setupViewInCell()
    }
    /// 添加view
    func addViewToCell(){
        //大图
        imgView.clipsToBounds = true
        imgView.contentMode = .scaleAspectFill
        addSubview(imgView)
        //标题
        titleView.textAlignment = .left
        addSubview(titleView)
        //作者
        authorView.numberOfLines = 1
        authorView.textAlignment = .left
        addSubview(authorView)
        //评论
        commentView.textAlignment = .right
        commentView.numberOfLines = 1
        addSubview(commentView)
        //底部分隔线
        bottomLine.backgroundColor = UIColor.cz_color(withRed: 163, green: 163, blue: 163)
        addSubview(bottomLine)
        for i in subviews{
            i.translatesAutoresizingMaskIntoConstraints = false
        }
        
    }
    
    /// 自动布局
    func setupViewInCell(){
        let margin = 10//边距
        let imgW = Int(UIScreen.cz_screenWidth()) - 2*margin//配图宽度
        let imgH = 65//配图高度
        let titleW = imgW//Int(UIScreen.cz_screenWidth()) - imgW - 3*margin
        
        
        //标题
        //        titleView.backgroundColor = UIColor.cz_random()
        titleView.snp.makeConstraints { (make) in
            make.top.equalTo(self).offset(margin)
            make.left.equalTo(self).offset(margin)
            make.width.equalTo(titleW)
        }
        //配图
        //        imgView.backgroundColor = UIColor.cz_random()
        imgView.snp.makeConstraints { (make) in
            make.left.equalTo(titleView)
            make.centerY.equalTo(self)
            make.width.equalTo(imgW)
            make.height.equalTo(imgW/2)
        }
        //作者
        //        authorView.backgroundColor = UIColor.cz_random()
        authorView.snp.makeConstraints { (make) in
            make.left.equalTo(titleView)
            make.bottom.equalTo(self).offset(-margin)
            make.right.equalTo(commentView.snp.left).offset(margin)
        }
        //评论
        //        commentView.backgroundColor = UIColor.cz_random()
        commentView.snp.makeConstraints { (make) in
            make.right.equalTo(titleView)
            make.bottom.equalTo(authorView)
            make.width.equalTo(imgH)
        }
        bottomLine.snp.makeConstraints { (make) in
            make.bottom.left.right.equalTo(self)
            make.height.equalTo(0.5)
        }
        
        
        
        
        
    }
    
    
}
